﻿'//DotNetNuke - http://www.dotnetnuke.com
'// Copyright (c) 2002-2010
'// by DotNetNuke Corporation
'//
'// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
'// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
'// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
'// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'//
'// The above copyright notice and this permission notice shall be included in all copies or substantial portions
'// of the Software.
'//
'// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
'// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
'// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
'// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
'// DEALINGS IN THE SOFTWARE.
Imports DotNetNuke.Modules.dnnsimplearticle.Components
Imports System.Globalization

Namespace Controls


    Partial Public Class ArticleList
        Inherits dnnsimplearticleModuleBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Try
                rptArticleList.DataSource = ArticleController.GetArticles(ModuleId, PageSize, PageNumber)
                rptArticleList.DataBind()
                If rptArticleList.Items.Count > 0 Then
                    BuildPageList(ArticleController.GetArticles(ModuleId, 1, 1)(0).TotalRecords)

                End If
            Catch exc As Exception
                Exceptions.ProcessModuleLoadException(Me, exc)
            End Try

        End Sub

        Private Sub BuildPageList(ByVal totalItems As Integer)
            Dim numberOfPages As Single = totalItems / CSng(PageSize)
            Dim intNumberOfPages As Integer = Convert.ToInt32(numberOfPages)
            If numberOfPages > intNumberOfPages Then
                intNumberOfPages += 1
            End If

            Dim queryString As NameValueCollection = Request.QueryString
            SetPagingLink(queryString, lnkNext, PageNumber + 1 < intNumberOfPages, PageNumber + 1, TabId)
            SetPagingLink(queryString, lnkPrevious, PageNumber - 1 > -1, PageNumber - 1, TabId)
        End Sub

        Private Shared Sub SetPagingLink(ByVal queryString As NameValueCollection, ByVal link As HyperLink, ByVal showLink As Boolean, ByVal linkedPageId As Integer, ByVal tabId As Integer)
            If showLink Then
                link.Visible = True
                queryString = New NameValueCollection(queryString)
                queryString("p") = linkedPageId.ToString(CultureInfo.InvariantCulture)
                Dim additionalParameters = New List(Of String)(queryString.Count)

                For i As Integer = 0 To queryString.Count - 1
                    If String.Equals(queryString.GetKey(i), "TABID", StringComparison.OrdinalIgnoreCase) Then
                        Dim newTabId As Integer
                        If Integer.TryParse(queryString(i), NumberStyles.[Integer], CultureInfo.InvariantCulture, newTabId) Then
                            tabId = newTabId
                        End If
                    ElseIf Not String.Equals(queryString.GetKey(i), "LANGUAGE", StringComparison.OrdinalIgnoreCase) Then
                        additionalParameters.Add(queryString.GetKey(i) + "=" + queryString(i))
                    End If
                Next


                link.NavigateUrl = DotNetNuke.Common.Globals.NavigateURL(tabId, String.Empty, additionalParameters.ToArray())
            Else
                link.Visible = False
            End If
        End Sub


    End Class
End Namespace
